#include <los_sys.h>
#include <linux/kernel.h>
#include <fcntl.h>

#include "hirandom.h"
#include "hisoc/random.h"


static int random_hw_open(struct file *filep)
{
    int ret = ENOERR;
    ret = hi_random_hw_init();
    return ret;
}

static int random_hw_close(struct file *filep)
{
    int ret = ENOERR;
    ret = hi_random_hw_deinit();
    return ret;
}

static int random_hw_ioctl(struct file *filep, int cmd, unsigned long arg)
{
    int ret = ENOERR;
    switch (cmd) {
        default:
            PRINT_ERR("!!!bad command!!!\n");
            return -EINVAL;
    }
    return ret;
}

static ssize_t random_hw_read(struct file *filep, char *buffer, size_t buflen)
{
    int ret = ENOERR;
    ret = hi_random_hw_getnumber(buffer, buflen);

    if (ENOERR == ret) {
        ret = buflen;
    }
    return ret;
}

static const struct file_operations_vfs random_hw_dev_ops = {
    random_hw_open,  /* open */
    random_hw_close, /* close */
    random_hw_read,  /* read */
    NULL, /* write */
    NULL,          /* seek */
    random_hw_ioctl,  /* ioctl */
#ifndef CONFIG_DISABLE_POLL
    0, /* poll */
#endif
    NULL
};

int random_hw_dev_register(void)
{
    int ret = 0;

    ret = hi_random_hw_support();
    if (ret) {
        return register_driver("/dev/random_hw", &random_hw_dev_ops, 0666, 0);
    } else {
        return -EPERM;
    }
}

